package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.VisitorDAO;

import entity.Visitor;

/**
 * Servlet implementation class RegisteredServerlet
 */
@WebServlet("/RegisteredServerlet")
public class RegisteredServerlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RegisteredServerlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String email,usn;
		Visitor vb=new Visitor();
		usn=request.getParameter("username");
		vb.setUserName(usn);
		vb.setFirstName(request.getParameter("firstname"));
		vb.setLastName(request.getParameter("lastname"));
		vb.setPassword(request.getParameter("password"));
		vb.setGender(request.getParameter("gender"));
		vb.setTelephone(request.getParameter("telephone"));
		email=request.getParameter("email");
		vb.setEmail(email);
		vb.setArea(request.getParameter("area"));
		vb.setHobby(request.getParameter("hobby"));
		vb.setDescription(request.getParameter("description"));
		vb.setRole((byte)0);
		if(!VisitorDAO.isduplicateInfo(vb)){
			if(VisitorDAO.addVisitor(vb)){
				RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/Search.jsp");
				RequetsDispatcherObj.forward(request, response);
			}else{
				RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/index.jsp");
				RequetsDispatcherObj.forward(request, response);
			}
		}else{
			RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/index.jsp");
			RequetsDispatcherObj.forward(request, response);
		}

	}

}
